package com.api.gbhg.huangwm.BxActionV2;

import com.alibaba.fastjson.JSONObject;
import com.customization.commons.LogTool;
import com.engine.common.util.ParamUtil;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2023-04-09
 * @Version 1.0
 * @Description TODO
 **/
@Path("/BxActionV2")
public class WebApi {

    private LogTool logTool = new LogTool("/log/gbhg/huangwm/BxActionV2/WebApi",false);

    private BxActionUtils actionUtils = new BxActionUtils();

    /**
     * 释放预算
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/releaseJingFei")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> releaseJingFei(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    //public Map<String, Object> releaseJingFei(Map<String, Object> params) {

        logTool.writeLog("=====释放经费额度&&单位使用额度=====");
        Map<String, Object> apiResult = new HashMap<String, Object>();
        apiResult.put("status", true);
        apiResult.put("msg", "success");
        Map<String, Object> params = ParamUtil.request2Map(request);
        logTool.writeLog(JSONObject.toJSONString(params));
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        RecordSet rs = new RecordSet();
        try{
            List<Map<String,Object>> datas = new ArrayList<>();
            String key = params.get("key").toString();
            String index = params.get("rowIndex").toString();
            String wfid = params.get("wfid").toString();
            String requestid = params.get("requestid").toString();
            String ysxm = params.get("ysxm").toString();
            String bmjjzcfl = params.get("bmjjzcfl").toString();
            String zfjjzcfl = params.get("zfjjzcfl").toString();
            Double sqbxje = Double.parseDouble(params.get("sqbxje").toString());
            Double cxje = Double.parseDouble(params.get("cxje").toString());
            Integer jbbm = Integer.parseInt(params.get("jbbm").toString());
            //删除uf_bxsjjl 表中的报销记录
            boolean check = rs.execute("select mxid from uf_bxsjjl where lcid="+requestid+" and mxid="+key) && rs.next();
            if(!check){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "释放经费冻结额度失败 查询报销记录uf_bxsjjl失败["+requestid+","+key+"]");
                return apiResult;
            }

            Map<String,Object> data = new HashMap<>();
            data.put("sqbxje",sqbxje-cxje);//申请报销金额-冲销金额
            data.put("ysxm",ysxm);
            data.put("bmjjzcfl",bmjjzcfl);
            datas.add(data);

            boolean result1 = actionUtils.releaseJingFei(rst,datas);
            if(!result1){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "释放经费冻结额度失败 更新jingfei表释放额度失败 ");
                return apiResult;
            }
            boolean result2 = actionUtils.releaseSydwed(rst,datas,jbbm);
            if(!result2){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "释放经费冻结额度失败 使用单位额度释放失败 ");
                return apiResult;
            }

            boolean result3 = rst.execute("delete from uf_bxsjjl where lcid="+requestid+" and ysxm="+ysxm+" and bmjjfl="+bmjjzcfl+" and sqbxje="+sqbxje+" and mxid="+key);
            if(!result3){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "释放经费冻结额度失败 删除报销记录uf_bxsjjl失败 ");
                return apiResult;
            }
            rst.commit();
            return apiResult;
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            apiResult.put("status", false);
            apiResult.put("msg", "释放经费冻结额度异常 "+e.getMessage());
            return apiResult;
        }
    }

    /**
     * 重新冻结预算
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/reduceJingFei")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> reduceJingFei(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    //public Map<String, Object> reduceJingFei(Map<String, Object> params) {

        logTool.writeLog("=====冻结经费额度&&单位使用额度=====");
        Map<String, Object> apiResult = new HashMap<String, Object>();
        apiResult.put("status", true);
        apiResult.put("msg", "success");
        Map<String, Object> params = ParamUtil.request2Map(request);
        logTool.writeLog(JSONObject.toJSONString(params));
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        RecordSet rs = new RecordSet();
        try{
            List<Map<String,Object>> datas = new ArrayList<>();
            String key = params.get("key").toString();
            String index = params.get("rowIndex").toString();
            String wfid = params.get("wfid").toString();
            String requestid = params.get("requestid").toString();
            String ysxm = params.get("ysxm").toString();
            String bmjjzcfl = params.get("bmjjzcfl").toString();
            String zfjjzcfl = params.get("zfjjzcfl").toString();
            Double sqbxje = Double.parseDouble(params.get("sqbxje").toString());
            Double cxje = Double.parseDouble(params.get("cxje").toString());
            Integer jbbm = Integer.parseInt(params.get("jbbm").toString());

            //查询uf_bxsjjl 表中的报销记录
            boolean check = rs.execute("select mxid from uf_bxsjjl where lcid="+requestid+" and mxid="+key) && rs.next();
            if(check){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "冻结经费额度失败 查询报销记录uf_bxsjjl已存在信息["+requestid+","+key+"]");
                return apiResult;
            }

            Map<String,Object> data = new HashMap<>();
            data.put("sqbxje",sqbxje-cxje);//申请报销金额-冲销金额
            data.put("ysxm",ysxm);
            data.put("bmjjzcfl",bmjjzcfl);
            datas.add(data);

            boolean result1 = actionUtils.reduceJingFei(rst,datas);
            if(!result1){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "冻结经费额度失败 更新jingfei表释放额度失败 ");
                return apiResult;
            }
            boolean result2 = actionUtils.reduceSydwed(rst,datas,jbbm);
            if(!result2){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "冻结经费额度失败 使用单位额度释放失败 ");
                return apiResult;
            }
            //添加uf_bxsjjl 表中的报销记录
            Object[] objects = {
                    1,requestid,key,ysxm,sqbxje,sqbxje,bmjjzcfl,zfjjzcfl,1,cxje
            };
            boolean result3 = rst.executeUpdate("insert into uf_bxsjjl(lclx,lcid,mxid,ysxm,sqbxje,tybxje,bmjjfl,zfjjfl,sfytj,cxje)\n" +
                    "values(?,?,?,?,?,?,?,?,?,?)",objects);
            if(!result3){
                rst.rollback();
                apiResult.put("status", false);
                apiResult.put("msg", "冻结经费额度失败 新增报销记录uf_bxsjjl失败 ");
                return apiResult;
            }
            rst.commit();
            return apiResult;
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            apiResult.put("status", false);
            apiResult.put("msg", "冻结经费额度异常 "+e.getMessage());
            return apiResult;
        }
    }
}
